package dbPicasso::UI::Dialog::dlgConnectionManager;

use strict;
use base qw(dbPicasso::UI::Dialog);

use Data::Dumper;

use dbPicasso::Config;

our $DIALOG;

sub show
{
    my $class = shift;
    $class->SUPER::show("dlgConnectionManager", __PACKAGE__, undef, \&init_dialog);
}

sub init_dialog
{
    my ($dialogobj, $dialogdef) = @_;

    $DIALOG = $dialogdef;

    &fill_connection_list(@_);
}

sub fill_connection_list
{
    my ($dialogobj, $dialogdef) = @_;

    my $namelist = $dialogdef->get_widget("Name");
    my $cxlist   = dbPicasso::Config->get_section("Connections");

    my @names = sort keys %{$cxlist};

    dbPicasso::UI->fill_simple_comboboxentry($namelist, \@names);
}

sub on_saveconnection_clicked
{
    my @widgetnames = qw(Name Driver Host UserID Password Database);

    my %cxinfo;

    map {
        my $widget = $DIALOG->get_widget($_);
        $cxinfo{$_} = $widget->get_text        if (ref($widget) eq 'Gtk2::Entry');
        $cxinfo{$_} = $widget->child->get_text if (ref($widget) eq 'Gtk2::ComboBoxEntry');
        $cxinfo{$_} = $widget->get_active_text if (ref($widget) eq 'Gtk2::ComboBox');
    } @widgetnames;

    my $cxlist = dbPicasso::Config->get_section("Connections");

    $$cxlist{$cxinfo{Name}} = \%cxinfo;
    delete $cxinfo{Name};    # redundant, since hash key is named the same

    dbPicasso::Config->save;

}

sub on_connectionmanager_activate
{

}

1;
